﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using BPM.Foundation.WFDB;
using BPM.Base.Tools;

namespace BPM.Foundation.WFService
{
    public partial interface IWorkflowService
    {
        [System.ServiceModel.OperationContract]
        int? createBusiness(string pid, WF_Business wf_Business);

        [System.ServiceModel.OperationContract]
        int? updateBusiness(string pid, WF_Business wf_Business);

        [System.ServiceModel.OperationContract]
        List<WF_Business> getBusiness(string pid, string departmentNo, string dutyNo, string userNo);

        [System.ServiceModel.OperationContract]
        BusinessTree getBusinessTree(string pid, string businessID);

        [System.ServiceModel.OperationContract]
        int? deleteBusiness(string pid, string businessID);

        [System.ServiceModel.OperationContract]
        List<WF_Instance_V> getProcessByBusinessID(string pid, string businessID);
    }
    public partial class WorkflowService : IWorkflowService
    {
        //[7.1	创建业务]
        public int? createBusiness(string pid, WF_Business wf_Business)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (wf_Business == null)
                {
                    return 1;
                }

                //[2]
                if (wf_Business.masterBusinessID == null)
                {
                    wf_Business.masterBusinessID = "";
                }


                //[3]
                wf_Business.ID = Guid.NewGuid();
                wf_Business.createTime = System.DateTime.Now;


                //[4]
                var v = wfDB.WF_Business.Count(p => p.businessID == wf_Business.businessID);
                if (v > 0)
                {
                    return 2;
                }

                //[5]
                wfDB.WF_Business.InsertOnSubmit(wf_Business);
                try
                {
                    wfDB.SubmitChanges();
                }
                catch (System.Exception ex)
                {
                    MyLog.writeError(ex.Message);
                }
                return null;
            }
        }

        //[7.2	修改业务]
        public int? updateBusiness(string pid, WF_Business wf_Business)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (wf_Business == null)
                {
                    return 1;
                }

                //[2]
                var v = wfDB.WF_Business.Count(p => p.businessID == wf_Business.businessID);
                if (v != 1)
                {
                    return 2;
                }

                var l = wfDB.WF_Business.Single(p => p.businessID == wf_Business.businessID);
                l.state = wf_Business.state;
                l.completeTime = wf_Business.completeTime;
                l.instanceID = wf_Business.instanceID;
                l.masterBusinessID = wf_Business.masterBusinessID;
                l.secrecy = wf_Business.secrecy;
                l.templateEdition = wf_Business.templateEdition;
                l.templateID = wf_Business.templateID;
                l.testID = wf_Business.testID;
                l.urgency = wf_Business.urgency;
                l.businessDescription = wf_Business.businessDescription;
                l.businessName = wf_Business.businessName;
                l.businessType = wf_Business.businessType;

                try
                {
                    wfDB.SubmitChanges();
                }
                catch (System.Exception ex)
                {
                    MyLog.writeError(ex.Message);
                }
                return null;
            }

        }

        //[7.3	得到的业务]
        public List<WF_Business> getBusiness(string pid, string departmentNo, string dutyNo, string userNo)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //1
                if (departmentNo == "" && dutyNo == "" && userNo == "")
                {
                    return wfDB.WF_Business.ToList();
                }

                //2
                if (departmentNo != "" && dutyNo == "" && userNo == "")
                {
                    return wfDB.WF_Business.Where(p => p.startDepartment == departmentNo).ToList();
                }

                //3
                if (departmentNo == "" && dutyNo != "" && userNo == "")
                {
                    return wfDB.WF_Business.Where(p => p.startDuty == dutyNo).ToList();
                }

                //4
                if (departmentNo == "" && dutyNo == "" && userNo != "")
                {
                    return wfDB.WF_Business.Where(p => p.startUser == userNo).ToList();
                }

                //5
                if (departmentNo != "" && dutyNo != "" && userNo == "")
                {
                    return wfDB.WF_Business.Where(p => p.startDepartment == departmentNo && p.startDuty == dutyNo).ToList();
                }

                //6
                if (departmentNo != "" && dutyNo != "" && userNo != "")
                {
                    return wfDB.WF_Business.Where(p => p.startDepartment == departmentNo && p.startDuty == dutyNo && p.startUser == userNo).ToList();
                }

                return null;
            }
        }

        //[7.4	得到的业务树]
        public BusinessTree getBusinessTree(string pid, string businessID)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (string.IsNullOrEmpty(businessID))
                {
                    return null;
                }
                //
                BusinessTree resultBusinessTree = new BusinessTree();
                WF_Business current = wfDB.WF_Business.SingleOrDefault(p => p.businessID == businessID);


                if (current != null)
                {
                    WF_Business parent = wfDB.WF_Business.SingleOrDefault(p => p.businessID == current.masterBusinessID);



                    resultBusinessTree.current = current;
                    resultBusinessTree.parent = parent;
                    resultBusinessTree.childList = new List<BusinessTree>();
                    addChildBusinessTree(resultBusinessTree);


                }
                return resultBusinessTree;
            }
        }

        //[7.5] 递归添加业务树子结点
        private void addChildBusinessTree(BusinessTree currentTree)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                List<WF_Business> childList = wfDB.WF_Business.Where(p => p.masterBusinessID == currentTree.current.businessID).ToList();

                foreach (WF_Business child in childList)
                {
                    BusinessTree bTree = new BusinessTree();
                    bTree.parent = currentTree.current;
                    bTree.current = child;
                    bTree.childList = new List<BusinessTree>();
                    currentTree.childList.Add(bTree);

                    addChildBusinessTree(bTree);

                }
            }


        }

        //[7.6	删除业务]
        public int? deleteBusiness(string pid, string businessID)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (String.IsNullOrEmpty(businessID))
                {
                    return 1;
                }
                var business = wfDB.WF_Business.SingleOrDefault(p => p.businessID == businessID);
                if (business != null)
                {
                    wfDB.WF_Business.DeleteOnSubmit(business);
                    try
                    {
                        wfDB.SubmitChanges();
                    }
                    catch (System.Exception ex)
                    {
                        MyLog.writeError(ex.Message);
                    }
                }
                return null;
            }

        }

        //[7.7 得到业务的流程]
        public List<WF_Instance_V> getProcessByBusinessID(string pid, string businessID)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                var v = wfDB.WF_Instance_V.Where(p => p.businessID == businessID);
                return v.ToList();
            }
        }

        //[7.8 根据业务ID和启动窗体得到数据ID]
        public WF_BusinessForm getWFBusinessForm(string pid, string businessId, string startWindow)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                return wfDB.WF_BusinessForm.SingleOrDefault(bf => (bf.businessID == businessId && bf.startWindow == startWindow));
            }
        }

        //[7.9 新增业务对应数据ID]
        public int? addWFBusinessForm(string pid, WF_BusinessForm wf_BusinessForm)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (string.IsNullOrEmpty(wf_BusinessForm.startWindow))
                    return 1;
                if (string.IsNullOrEmpty(wf_BusinessForm.businessID))
                    return 2;
                if (string.IsNullOrEmpty(wf_BusinessForm.dataRowID))
                    return 3;
                wfDB.WF_BusinessForm.InsertOnSubmit(wf_BusinessForm);
                wfDB.SubmitChanges();
                return null;
            }
        }

        //[7.9 新增业务对应数据ID]
        public int? updateWFBusinessForm(string pid, WF_BusinessForm wf_BusinessForm)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (string.IsNullOrEmpty(wf_BusinessForm.startWindow))
                    return 1;
                if (string.IsNullOrEmpty(wf_BusinessForm.businessID))
                    return 2;
                if (string.IsNullOrEmpty(wf_BusinessForm.dataRowID))
                    return 3;
                Expression<Func<WF_BusinessForm, bool>> expr = PredicateExtensions.True<WF_BusinessForm>();
                expr.And(bf => bf.businessID == wf_BusinessForm.businessID);
                expr.And(bf => bf.startWindow == wf_BusinessForm.startWindow);
                var result= wfDB.WF_BusinessForm.SingleOrDefault(expr);
                result.dataRowID = wf_BusinessForm.dataRowID;
                wfDB.SubmitChanges();
                return null;
            }
        }

        public WF_Instance getWFInstance(string pid, Guid instanceID)
        {
            if (instanceID==null)
                return null;
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                return wfDB.WF_Instance.SingleOrDefault(i=>i.instanceID==instanceID);
            }
        }
    }
}
